/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package nut.com;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author Nut
 */
public class FeedSearcher2 extends HttpServlet {

    XMLEventReader reader;
    String tmp;
    ArrayList<String> liGet;
    ArrayList<String> titleGet;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String input = request.getParameter("url");
        String key = request.getParameter("key").toLowerCase();

        boolean titleFound = false;
        boolean linkFound = false;
        boolean itemFound = false;
        boolean searched = false;
        liGet = new ArrayList<String>();
        titleGet = new ArrayList<String>();
        String eName;
        try {
            //set proxy for KKU network
            System.setProperty("http.proxyHost", "10.10.10.1");
            System.setProperty("http.proxyPort", "8088");
            URL u = new URL(input);
            InputStream in = u.openStream();
            XMLInputFactory factory = XMLInputFactory.newInstance();
            reader = factory.createXMLEventReader(in);
            out.print("<html><body><table  align='center' border='1'><tr><th>Title</th><th>Link</th></tr>");
            while (reader.hasNext()) {
                XMLEvent event = reader.nextEvent();
                if (event.isStartElement()) {
                    StartElement element = (StartElement) event;
                    eName = element.getName().getLocalPart();
                    if (eName.equals("item")) {
                        itemFound = true;
                    }
                    if (itemFound && eName.equals("link")) {
                        linkFound = true;
                    }
                    if (itemFound && eName.equals("title")) {
                        titleFound = true;
                    }
                }
                if (event.isEndElement()) {
                    EndElement element = (EndElement) event;
                    eName = element.getName().getLocalPart();
                    if (eName.equals("item")) {
                        itemFound = false;
                    }
                    if (itemFound && eName.equals("link")) {
                        linkFound = false;
                    }
                    if (itemFound && eName.equals("title")) {
                        titleFound = false;
                    }
                }
                if (event.isCharacters()) {
                    Characters characters = (Characters) event;

                    if (titleFound && (characters.getData().contains(key))) {
                        tmp = characters.getData();
                        titleGet.add(tmp);
                        searched = true;
                    }
                    if (linkFound && searched) {
                        liGet.add("<tr><td>" + titleGet.get(0) + "</td><td><a href='" + characters.getData() + "'>" + characters.getData() + "</a></td></tr>");
                        searched = false;
                        titleGet.clear();
                    }
                }
            }
            reader.close();
            for (String all : liGet) {
                out.println(all);
            }
            out.print("</table></body></html>");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }
}
